<?php

namespace api\modules\manage\models;

use api\modules\manage\components\Transfer;
use api\modules\manage\resources\ElementResource;
use api\modules\manage\services\MenuService;
use Yii;

/**
 * This is the model class for table "dict_elements".
 *
 * @property integer $id
 * @property string $code
 * @property integer $type
 * @property string $name
 * @property string $description
 * @property string $ext
 * @property integer $state
 */
class DictElements extends \api\modules\manage\models\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'dict_elements';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('test_3');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['code', 'type', 'name'], 'required'],
            [['type', 'state'], 'integer'],
            [['description'], 'string'],
            [['code'], 'string', 'max' => 50],
            [['name'], 'string', 'max' => 100],
            [['ext'], 'string', 'max' => 5000],
            [['code', 'type'], 'unique', 'targetAttribute' => ['code', 'type'], 'message' => 'The combination of Code and Type has already been taken.'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'code' => 'Code',
            'type' => 'Type',
            'name' => 'Name',
            'description' => 'Description',
            'ext' => 'Ext',
            'state' => 'State',
        ];
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if(!$insert){
                if($this->state == 0){
                    switch ($this->type){
                        case ElementResource::TYPE_CARD:
                        case ElementResource::TYPE_INFO:
                        case ElementResource::TYPE_PRINT:
                            $link = BusinessLinks::findOne(['link_id' => $this->id]);
                            break;
                        case ElementResource::TYPE_FILTER:
                        case ElementResource::TYPE_SORT:
                        case ElementResource::TYPE_FLOW:
                        case ElementResource::TYPE_STATUS:
                            $link = MenusLinks::findOne(['link_id' => $this->id]);
                            break;
                        case ElementResource::TYPE_MENU:
                            $link = MenusLinks::findOne(['menu_id' => $this->id]);
                            break;
                        case ElementResource::TYPE_NOTIFICATION:
                            $link = NotificationBind::findOne(['tmp_id' => $this->id]);
                            break;
                        default:
                            $link = null;
                    }
                    if ($link){
                        throw new \Exception(Yii::t('app','不可删除，请先解绑配置项。'));
                    }
                }
            }
            return true;
        }else{
            return false;
        }
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if (in_array($this->type,[ElementResource::TYPE_FLOW,ElementResource::TYPE_MENU])) {
            Transfer::push2RightQueue();
        }
    }
}